package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐管理类")
public class SetmealController {

    @Autowired
    private SetmealService  setmealService;

    @ApiOperation("新增套餐")
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache" , key = "#dto.categoryId")
    public Result saveSetmeal(@RequestBody SetmealDTO dto) {
        setmealService.saveSetmeal(dto);
        return Result.success();

    }

    @ApiOperation("分页查询套餐")
    @GetMapping("/page")
    public Result getSetmeals(SetmealPageQueryDTO dto){
        PageResult pageResult = setmealService.getSetmeals(dto);
        return  Result.success(pageResult);
    }

    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result getSetmealById(@PathVariable Integer id) {
        SetmealVO setmealVO = setmealService.getSetmealById(id);
        return Result.success(setmealVO);
    }

    @ApiOperation("修改套餐")
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache" , allEntries = true)
    public Result updateSetmeal(@RequestBody SetmealDTO dto) {
        setmealService.updateSetmeal(dto);
        return Result.success();
    }

    @ApiOperation("根据ids删除套餐以及中间表信息")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache" , allEntries = true)
    public Result deleteSetmeals(@RequestParam List<Long> ids) {
        setmealService.deleteSetmeals(ids);
        return Result.success();
    }

    @ApiOperation("修改套餐起售停售状态")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache" , allEntries = true)
    public Result updateSetmealStatus(@PathVariable Integer status, Long id) {
        setmealService.updateSetmealStatus(status,id);
        return Result.success();
    }
}
